package example.dawangba.com.exp4_part2_z09315222;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;

public class ResultActivity extends AppCompatActivity {

    TextView tv_name;
    TextView tv_content;
    ImageView imageView;

    int getMonth;
    int getDay;

    private String[] constellationFiles = {"star12.txt","star1.txt","star2.txt","star3.txt","star4.txt",
            "star5.txt","star6.txt","star7.txt","star8.txt","star9.txt","star10.txt",
            "star11.txt"};

    private int[] constellationImages = {R.mipmap.star12,R.mipmap.star1,R.mipmap.star2,R.mipmap.star3,R.mipmap.star4,
    R.mipmap.star5,R.mipmap.star6,R.mipmap.star7,R.mipmap.star8,R.mipmap.star9,
            R.mipmap.star10,R.mipmap.star11};

    String[] constellationName = new String[]{"摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座",
            "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"};

    int[] constellationDivider = new int[]{20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);

        tv_name = (TextView) findViewById(R.id.userName);
        tv_content = (TextView) findViewById(R.id.constellation_text);
        imageView = (ImageView) findViewById(R.id.constellation_image);

        Bundle bundle = this.getIntent().getExtras();

        tv_name.setText(bundle.getString("userName"));

        getMonth = bundle.getInt("month");
        getDay = bundle.getInt("day");

        getConstellation(getMonth,getDay);

    }

    private String getConstellation(int month, int day){
        int index = getMonth;

        if (month ==12 && day >= 22){
            imageView.setImageResource(R.mipmap.star12);
            String str = readFromAssets("star12.txt");
            tv_content.setText(str);
        } else {
            if (day < constellationDivider[month - 1]){
                index = index -1;
            }

            Log.e("wugui", String.valueOf(month)+" "+String.valueOf(day)+" "+String.valueOf(index));

            imageView.setImageResource(constellationImages[index]);

            String str = readFromAssets(constellationFiles[index]);
            tv_content.setText(str);
        }
        return constellationName[index];
    }

    public String readFromAssets(String fileName){
        String result = "";
        try {
            InputStream in = getResources().getAssets().open(fileName);
            byte[] buffer = new byte[in.available()];
            in.read(buffer);
            result = new String(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
